package com.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateUtils {
	/** 
	* @Title: getCurrentDay 
	* @Description: TODO(获取当前时间) 
	* @param @param format
	* @param @return    设定文件 
	* @return String    返回类型 
	* @throws 
	*/
	public static String getCurrentDay(String format){
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(new Date());
	}
	
	/** 
	* @Title: getBefore15Min 
	* @Description: TODO(前15分钟整点) 
	* @param @param format
	* @param @return    设定文件 
	* @return String    返回类型 
	* @throws 
	*/
	public static String getBefore15Min(String format){
		GregorianCalendar gc = new GregorianCalendar();
		gc.set(Calendar.MINUTE, (gc.get(Calendar.MINUTE))/15*15);
		return new SimpleDateFormat(format).format(gc.getTime());
	}
	/** 
	 * @Title: getBefore5Min 
	 * @Description: TODO(前5分钟整点) 
	 * @param @param format
	 * @param @return    设定文件 
	 * @return String    返回类型 
	 * @throws 
	 */
	public static String getBefore5Min(String format){
		GregorianCalendar gc = new GregorianCalendar();
		gc.set(Calendar.MINUTE, gc.get(Calendar.MINUTE)/5*5);
		return new SimpleDateFormat(format).format(gc.getTime());
	}
	public static String getAfterTime(String format,int type,int num){
		GregorianCalendar gc = new GregorianCalendar();
		gc.add(type, num);
		return new SimpleDateFormat(format).format(gc.getTime());
	}
	
	/** 
	* @Title: getBefore30Min 
	* @Description: TODO(前30分钟整点) 
	* @param @param format
	* @param @return    设定文件 
	* @return String    返回类型 
	* @throws 
	*/
	public static String getBefore30Min(String format){
		GregorianCalendar gc = new GregorianCalendar();
		gc.set(Calendar.MINUTE, gc.get(Calendar.MINUTE)/30*30);
		return new SimpleDateFormat(format).format(gc.getTime());
	}
	
	/** 
	* @Title: orderTimeFormat 
	* @Description: TODO(整理时间) 
	* @param @param time
	* @param @return    设定文件 
	* @return String    返回类型 
	* @throws 
	*/
	public static String orderTimeFormat(String time){
		try {
			String[] times = time.split("_");
			if(times==null || times.length!=2){
				times = time.split("\\s+");
			}
			if(times!=null && times.length==2){
				Calendar cld = Calendar.getInstance();
				String[] date = times[0].split("-");
				cld.set(Calendar.YEAR, Integer.parseInt(date[0]));
				cld.set(Calendar.MONTH, Integer.parseInt(date[1])-1);
				cld.set(Calendar.DATE, Integer.parseInt(date[2]));
				String[] t = times[1].split(":");
				cld.set(Calendar.HOUR_OF_DAY, Integer.parseInt(t[0]));
				cld.set(Calendar.MINUTE, Integer.parseInt(t[1]));
				return new SimpleDateFormat("yyyy-MM-dd HH:mm:00").format(cld.getTime());
			}else{
				System.out.println("时间"+time+"不符合格式x-x-x x:x:x要求");
			}
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("时间"+time+"整理失败，异常信息:"+e.getMessage());
		}
		return "error";
	}
	
	public static void main(String[] args) {
		System.out.println(getCurrentDay("HH:mm:ss"));
	}
}
